package vue;

import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.*;

import javax.swing.JOptionPane;
import javax.swing.JPanel;

import controller.Controleur;
import exception.FinDePartie;
import exception.SelectionImpossible;

import model.Case;


/**
 * Class AffichePrincipalePlateau
 */
public class AffichePrincipalePlateau extends JPanel implements ObserveurPlateau, MouseListener {

  //
  // Fields
  //
	private DessinCase cases[][];
	private int x, y, size=60;
	

  
  //
  // Constructors
  //
  public AffichePrincipalePlateau (int x, int y) {
	
	  super(new GridLayout(x,y,0,0));
	  this.x=x;
	  this.y=y;
	  setSize(x*size,y*size);
	  cases = new DessinCase[x][y];
	  for(int i=0;i<8;i++)
		  for(int j=0;j<8;j++){
			  cases[j][i]=new DessinCase(j,i);
			  add(cases[j][i]);
			  }
	  addMouseListener(this);
  }



@Override
public void Modification(List<Case> Modif) {
	for (Case uneCase : Modif)	{
		if (uneCase!=null)
			cases[uneCase.getX()][uneCase.getY()].dessine(uneCase);
	}
}



@Override
public void mouseClicked(MouseEvent arg0) {
	int x = arg0.getX()/size;
	int y = arg0.getY()/size;
	try {
		Controleur.getControleur().selectionnerCase(x, y);
	} catch (SelectionImpossible e) {
		e.printStackTrace();
	} catch (FinDePartie e) {
		JOptionPane.showMessageDialog(null, e.getMessage(), null, 0);
	}
}



@Override
public void mouseEntered(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}



@Override
public void mouseExited(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}



@Override
public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}



@Override
public void mouseReleased(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
}
  

}
